package my_utils;

import java.util.Scanner;

public class MyTools {
    static Scanner scanner = new Scanner(System.in);

    public static String checkName() {
        while (true) {
            String name = scanner.next();
            if (name.length() > 1 && name.length() <= 40) {
                return name;
            } else {
                System.out.print("名字长度应在1~40之间，请重新输入：");
            }
        }
    }

    public static char checkGender() {
        while (true) {
            char gender = scanner.next().charAt(0);
            if (gender == '男' || gender == '女') {
                return gender;
            } else {
                System.out.print("你输入的性别（男/女）错误，请重新输入：");
            }
        }
    }

    public static int checkAge() {
        while (true) {
            int age = scanner.nextInt();
            if (age > 0 && age <= 120) {
                return age;
            } else {
                System.out.print("你输入的年龄（1-120）有误，请重新输入：");
            }
        }
    }

    public static String checkId() {
        while (true) {
            String id = scanner.next();
            if (id.length() >= 8 && id.length() <= 15) {
                return id;
            } else {
                System.out.print("你输入的学号/学工号（8-15）有误，请重新输入：");
            }
        }
    }

    public static String checkPassword() {
        while (true) {
            String password = scanner.next();
            if (password.length() >= 6 && password.length() < 20) {
                return password;
            } else {
                System.out.print("你输入的密码有误，密码长度（6-20），请重新输入：");
            }
        }
    }

    public static String checkPhoneNum() {
        while (true) {
            String phoneNum = scanner.next();
            if (phoneNum.length() == 11) {
                return phoneNum;
            } else {
                System.out.print("手机号错误，长度应为11位，请重新输入：");
            }
        }
    }

    public static String checkClass() {
        while (true) {
            String class_ = scanner.next();
            if (class_.length() > 1 && class_.length() < 30) {
                return class_;
            } else {
                System.out.println("班级长度（1-30）之间，示例：1班");
            }
        }
    }

    public static double checkScore() {
        while (true) {
            double score = scanner.nextDouble();
            if (score >= 0.0 && score <= 100.0) {
                return score;
            } else {
                System.out.print("成绩应在0-100之间，请重新输入：");
            }
        }
    }

    public static int checkNum() {
        while (true) {
            int num = scanner.nextInt();
            if (num >= 0) {
                return num;
            } else {
                System.out.print("输入的编号(大于或等于0)，请重新输入：");
            }
        }
    }

    //确认删除
    public static char confirmDel() {
        System.out.print("请确认是否删除（Y/N），请小心选择：");
        char confirm;
        while (true) {
            confirm = toUpper(scanner.next().charAt(0));
            if (confirm == 'Y'|| confirm == 'N') {
                break;
            } else {
                System.out.println("输入错误，请重新输入（Y/N）");
            }
        }
        return confirm;
    }

    //小写转大写
    public static char toUpper(char c){
        if(c >= 'a' && c <= 'z'){
            return (char)(c-32);
        }else{
            return c;
        }
    }

    //退出
    public static boolean exit() {
        String choice = "";
        while (true) {//该循环主要完成 必须输入正确的y/n，否则循环输入指令
            System.out.println("你确认要退出吗？y/n");
            choice = scanner.next();
            if ("y".equals(choice) || "n".equals(choice)) {
                break;
            }
        }
        //判断
        if (choice.equals("n")) {
            return true;
        }
        return false;
    }

}
